package com.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.hospital.pojo.Ghxxb;
import com.hospital.pojo.Zyxxb;
import com.hospital.service.HospInitQueryService;
import com.hospital.service.ZhuYuanService;
import com.hospital.vo.ResultVO;
import com.hospital.vo.ZhuYuanQueryVo;
import com.hospital.vo.ZhuYuanResultVo;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.Resources;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class ZhuYuanController {
    @Resource
    private ZhuYuanService zhuYuanService;

    @Resource
    private HospInitQueryService hospInitQueryService;

    @InitBinder
    protected void initDateFormatBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @RequestMapping(value = "/zhuyuan/list", method = RequestMethod.GET)
    @ResponseBody
    public ResultVO<ZhuYuanResultVo> getAllZhuyuanInfo(Integer pageNumInput, ZhuYuanQueryVo vo){
        System.out.println(vo);
        if (pageNumInput == null) pageNumInput = 1;
        PageInfo<ZhuYuanResultVo> allZyxx = zhuYuanService.getAllZyxx(pageNumInput, 5, vo);
        ResultVO<ZhuYuanResultVo> resultVO = new ResultVO<>();
        resultVO.setPageInfo(allZyxx);
        return resultVO;
    }

    @RequestMapping(value = "/zhuyuandetail/{blh}", method = RequestMethod.GET)
    @ResponseBody
    public ResultVO<ZhuYuanResultVo> getAllZhuyuanDetail(@PathVariable("blh") Integer blh){
        ZhuYuanResultVo zyxxDetail = zhuYuanService.getZyxxDetail(blh);
        ResultVO<ZhuYuanResultVo> zhuYuanResultVoResultVO = new ResultVO<>();
        zhuYuanResultVoResultVO.setObj(zyxxDetail);
        return zhuYuanResultVoResultVO;
    }

    @RequestMapping(value = "/updateZyxx", method = RequestMethod.PUT)
    @ResponseBody
    public ResultVO<Object> updateZyxx(Ghxxb ghxxb, Zyxxb zyxxb){
        System.out.println("ghuahao");
        System.out.println(ghxxb);
        int ghxx = hospInitQueryService.updateGhxxb(ghxxb);

        System.out.println("zhuyuan");
        System.out.println(zyxxb);
        int zyxx = zhuYuanService.updateZyxxb(zyxxb);
        ResultVO<Object> vo = new ResultVO<>();
        if (ghxx != 1 || zyxx != 1){
            vo.setMessage("更新信息失败");
            vo.setCode(500);
        } else{
            vo.setMessage("更新信息成功");
        }
        return vo;
    }

    @RequestMapping(value = "/tuiyuan", method = RequestMethod.PUT)
    @ResponseBody
    public ResultVO<Object> tuiYuan(Integer blh){
        Ghxxb ghxxb = new Ghxxb();
        ghxxb.setBlh(blh);
        ghxxb.setZt(5);
        int updateGhxxb = hospInitQueryService.updateGhxxb(ghxxb);
        ResultVO<Object> resultVO = new ResultVO<>();
        if (updateGhxxb != 1){
            resultVO.setCode(500);
            resultVO.setMessage("退院失败，请重试或联系管理员");
        }
        return resultVO;
    }

    @RequestMapping(value = "/chuyuan", method = RequestMethod.PUT)
    @ResponseBody
    public ResultVO<Object> chuYuan(Integer blh){
        Ghxxb ghxxb = new Ghxxb();
        ghxxb.setBlh(blh);
        ghxxb.setZt(3);
        int updateGhxxb = hospInitQueryService.updateGhxxb(ghxxb);
        ResultVO<Object> resultVO = new ResultVO<>();
        if (updateGhxxb != 1){
            resultVO.setCode(500);
            resultVO.setMessage("出院失败，请重试或联系管理员");
        }
        return resultVO;
    }

    @RequestMapping(value = "/multiTuiyuan", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO<Object> multiTuiYuan(@RequestBody Map<String, ArrayList<Integer>> data) {
        System.out.println(data);
        List blhs = (ArrayList) data.get("blhs");
        System.out.println(blhs);
        ResultVO<Object> vo = new ResultVO<>();
        int i = hospInitQueryService.multiTuiyuan(blhs);
        if (i < 1){
            vo.setCode(500);
            vo.setMessage("批量退院失败");
        }
        vo.setMessage("批量退院成功");
        return vo;
    }

    @RequestMapping(value = "/multiChuyuan", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO<Object> multiChuyuan(@RequestBody Map<String, ArrayList<Integer>> data) {
        System.out.println(data);
        List blhs = data.get("blhs");
        System.out.println(blhs);
        ResultVO<Object> vo = new ResultVO<>();
        int i = hospInitQueryService.multiChuyuan(blhs);
        if (i < 1){
            vo.setCode(500);
            vo.setMessage("批量出院失败");
        }
        vo.setMessage("批量出院成功");
        return vo;
    }

    @RequestMapping(value = "/addZyxx", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO<Object> addZyxx(Ghxxb ghxxb, Zyxxb zyxxb){
        System.out.println(ghxxb);
        zyxxb.setBlh(ghxxb.getBlh());
        System.out.println(zyxxb);
        ghxxb.setZt(2);
        int i = zhuYuanService.addZyxx(zyxxb);
        hospInitQueryService.updateGhxxb(ghxxb);
        ResultVO<Object> vo = new ResultVO<>();
        if (i == 1){
            vo.setMessage("住院成功");
        } else {
            if (i == -1){
                vo.setMessage("住院信息已存在");
            }else{
                vo.setMessage("住院失败");
            }
            vo.setCode(500);
        }
        return vo;
    }
}
